home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
atlantis
/
atlantis.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
8KB
|
378 lines
/*
* Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
#include <gl.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <device.h>
#include "atlantis.h"
#include <sys/types.h>
#include <sys/times.h>
#include <sys/param.h>
struct fish{
float x,y,z,phi,theta,psi,v;
float xt,yt,zt;
float htail,vtail;
float dtheta;
int spurt,attack;
};
/* define black RGB color */
float blackvec[3] = {0.0, 0.0, 0.0};
Matrix idmat = { /* identity matrix */
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
float backpoly[4][3] = {
-4000, 4000,-10000,
4000, 4000,-10000,
4000,-4000,-10000,
-4000,-4000,-10000,
};
def_simple_light_calc()
{
static float spot[] ={
LCOLOR,1,1,1,
POSITION,0.0,1.0,0.0,0.0,
LMNULL};
static float mat1[] ={
AMBIENT,.0,.10,.2,
DIFFUSE,.460,.660,.795,
SPECULAR,.8,.8,.8,
SHININESS,90,
LMNULL};
static float lm[] ={
AMBIENT,.4,.4,.4,
LOCALVIEWER,0,
LMNULL};
lmdef(DEFMATERIAL, 1, 0, mat1);
lmdef(DEFLIGHT, 11, 0, spot);
lmdef(DEFLMODEL, 1, 0, lm);
}
use_simple_light_calc()
{
lmbind(LIGHT0, 11);
lmbind(LMODEL, 1);
}
struct fish sharky[NO_SHARKS];
struct fish whaley;
struct fish babywhale;
struct fish dolph;
float delt = 0.05;
fish_transform(struct fish *tfish)
{
translate(tfish->y,tfish->z,-tfish->x);
rot(-tfish->psi, 'y');
rot(tfish->theta, 'x');
rot(-tfish->phi, 'z');
}
void usage(void)
{
fprintf(stderr, "Usage: atlantis [-b] [-/+t]\n");
fprintf(stderr, " -b black background\n");
fprintf(stderr, " -t turn off texturing\n");
fprintf(stderr, " +t turn on texturing\n");
exit(1);
}
main(int argc, char **argv)
{
long dev;
short val;
int i,j;
float *dummy;
char version[12];
int black;
int texturing;
extern struct fish sharky[NO_SHARKS];
black = 0;
texturing = -1;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch(argv[i][1]) {
case 'b':
black = 1;
break;
case 't':
texturing = 0;
break;
default:
usage();
break;
}
} else if (argv[i][0] == '+') {
switch(argv[i][1]) {
case 't':
texturing = 1;
break;
default:
usage();
break;
}
} else usage();
}
srand(time(NULL));
for ( i = 0; i < NO_SHARKS; i++) {
sharky[i].x = 70000. + rand()%6000;
sharky[i].y = rand()%6000;
sharky[i].z = rand()%6000;
sharky[i].psi = rand()%360 - 180.;
sharky[i].v = 1.;
}
dolph.x = 30000.0;
dolph.z = 6000.0;
dolph.psi = 90.0;
dolph.theta = 0.0;
dolph.v = 3.0;
whaley.x = 70000.0;
whaley.y = 0.0;
whaley.psi = 90.0;
whaley.theta = 0.0;
whaley.v = 3.0;
babywhale.x = 60000.0;
babywhale.y = -2000.0;
babywhale.z = -2000.0;
babywhale.psi = 90.0;
babywhale.theta = 0.0;
babywhale.v = 3.0;
winopen("atlantis");
RGBmode();
doublebuffer();
gconfig();
if (getgdesc(GD_MULTISAMPLE) == 1) {
zbsize(0);
mssize(8,32,0);
gconfig();
if (getgconfig(GC_BITS_MS_ZBUFFER) == 0) {
zbsize(32);
mssize(0,0,0);
gconfig();
}
}
subpixel(TRUE);
cursoff();
lsetdepth(0,0x7fffff);
qdevice(LEFTMOUSE);
qdevice(MIDDLEMOUSE);
qdevice(RIGHTMOUSE);
qdevice(ESCKEY);
mmode(MPROJECTION);
perspective(400, 1.0, 100.0, 2000000.);
mmode(MVIEWING);
loadmatrix(idmat);
def_simple_light_calc();
lmbind(LIGHT0, 11);
use_simple_light_calc();
if (texturing == -1) {
gversion(version);
if (strncmp(version, "GL4DRE", 6) == 0 ||
strncmp(version, "GL4DVGX", 7) == 0) {
texturing = 1;
} else {
texturing = 0;
}
}
if (texturing) {
texinit();
}
init_timer();
for (;;) {
if (black) {
cpack(0x000000);
} else {
cpack(0x953535);
}
clear();
zclear();
mmode(MPROJECTION);
perspective(400, 1.0, 100.0, 2000000.);
mmode(MVIEWING);
loadmatrix(idmat);
look_about();
if(qtest()) {
switch(dev=qread(&val)) {
case REDRAW:
reshapeviewport();
break;
case ESCKEY:
if (val) exit(0);
break;
}
}
lmbind(MATERIAL,1);
texbind(TX_TEXTURE_0,1);
texgen(TX_T, TG_ON, dummy);
texgen(TX_S, TG_ON, dummy);
for (i = 0; i < NO_SHARKS; i++) {
pushmatrix();
sharkopilot(&sharky[i]);
sharkomiss(i);
fish_transform(&sharky[i]);
draw_shark(&sharky[i]);
popmatrix();
}
pushmatrix();
whalopilot(&dolph);
dolph.phi++;
fish_transform(&dolph);
draw_dolphin(&dolph);
popmatrix();
pushmatrix();
whalopilot(&whaley);
whaley.phi++;
fish_transform(&whaley);
draw_whale(&whaley);
popmatrix();
whalopilot(&babywhale);
babywhale.phi++;
fish_transform(&babywhale);
pushmatrix();
scale(0.45,0.45,0.3);
draw_whale(&babywhale);
popmatrix();
texgen(TX_T, TG_OFF, dummy);
texgen(TX_S, TG_OFF, dummy);
texbind(TX_TEXTURE_0,0);
swapbuffers();
}
}
sharkomiss(i)
int i;
{
extern struct fish sharky[NO_SHARKS];
int j;
float avoid,thetal;
float X,Y,Z,R;
for( j = 0; j < NO_SHARKS; j++) {
if( j != i) {
X = sharky[j].x - sharky[i].x;
Y = sharky[j].y - sharky[i].y;
Z = sharky[j].z - sharky[i].z;
R = sqrt(X * X + Y * Y + Z * Z);
avoid = 1.0;
thetal = sharky[i].theta;
if(R < SHARKSIZE) {
if(Z > 0.0) {
sharky[i].theta -= avoid;
} else {
sharky[i].theta += avoid;
}
}
sharky[i].dtheta += (sharky[i].theta - thetal);
}
}
}
static int booger = 0;
static float xe,ye,ze,phie,thetae,psie,ve;
look_about()
{
ve = 0.5 * (getvaluator(MOUSEY) - 512);
if(getbutton(F12KEY)) {
booger = 1 - booger;
while(getbutton(F12KEY)){}
}
if(booger) {
if (getbutton(LEFTMOUSE)) psie--;
if (getbutton(RIGHTMOUSE)) psie++;
if (getbutton(MIDDLEMOUSE)) {
xe += 10 * ve * cos(psie * 0.01745);
ye -= 10 * ve * sin(psie * 0.01745);
}
}
rot(phie,'z');
rot(-thetae,'x');
rot(psie,'y');
translate(ye,-ze,xe);
}
static struct tms tms_start_buf, tms_end_buf;
static int time_last, time_end;
init_timer()
{
time_end = times (&tms_end_buf);
}
int oj = 0;
int updater;